package cn.easybuy.service.impl;

import cn.easybuy.entity.PageInfo;
import cn.easybuy.entity.Product;
import cn.easybuy.entity.ProductCategory;
import cn.easybuy.service.*;
import cn.easybuy.dao.*;
import cn.easybuy.dao.impl.*;

import java.sql.SQLException;
import java.util.List;

public class ProductServiceImpl implements ProductService {
    private ProductDAO pd = new ProductDAOImpl();
    @Override
    public PageInfo<Product> getProListAll(PageInfo<Product> pageInfo) throws SQLException {
        return pd.proListAll(pageInfo);
    }

    @Override
    public  PageInfo<ProductCategory> getCategoryList( PageInfo<ProductCategory> page) throws SQLException {
        return pd.getCategoryList(page);
    }

    @Override
    public boolean categoryAdd(int cate1,int cate2, String name) throws SQLException {
        int i = pd.categoryAdd(cate1, cate2, name);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<ProductCategory> getCate1(int pid, int type) throws SQLException {
        return pd.getCate1(pid, type);
    }

    @Override
    public int categoryDel(int id) throws SQLException {
        return pd.categoryDel(id);
    }

    @Override
    public int productAdd(Product product) throws SQLException {
        return pd.productAdd(product);
    }

    @Override
    public Product getProductById(int id) throws SQLException {
        return pd.getProductById(id);
    }

    @Override
    public int productUpd(Product product) throws SQLException {
        return pd.productUpd(product);
    }

    @Override
    public int productDel(int id) throws SQLException {
        return pd.productDel(id);
    }
    private ProductDAO productDAO = new ProductDAOImpl();

    @Override
    public List<ProductCategory> getALlCate() throws SQLException {
        ProductCategory p = new ProductCategory();
        p.setParentId(0);
        p.setType(1);
        List<ProductCategory> list = getList(p);
        return list;
    }

    private List<ProductCategory> getList(ProductCategory p) throws SQLException {
        List<ProductCategory> list = productDAO.getALlCate(p);
        for (ProductCategory category : list) {
            category.setType(category.getType()+1);
            category.setParentId(category.getId());
            if(category.getType()>=4){
                break;
            }else{
                List<ProductCategory> list1 = getList(category);
                category.setCategoryList(list1);
            }
        }
        return list;
    }

    public List<Product> getProDuctList(int cateId) throws SQLException{
        return productDAO.getProDuctList(cateId);
    }

    @Override
    public Product getProById(int id) throws SQLException {
        return productDAO.getProById(id);
    }

    @Override
    public List<Product> getProListByCate2(int id,int type) throws SQLException {
        return pd.getProListCate2(id,type);
    }


}
